package com.mercury.ec.framework.air.core.client.impl;

import com.mercury.ec.framework.air.core.client.AirClient;
import com.mercury.ec.framework.air.core.property.AirChannelProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * 机票客户端的抽象类，提供模板方法，减少子类的冗余代码
 *
 * @author hesx
 */
@Slf4j
public abstract class AbstractAirClient implements AirClient {

    /**
     * 机票渠道配置
     */
    protected volatile AirChannelProperties properties;

    public AbstractAirClient(AirChannelProperties properties) {
        this.properties = properties;
    }

    /**
     * 初始化
     */
    public final void init() {
        doInit();
        log.debug("[init][配置({}) 初始化完成]", properties);
    }

    /**
     * 自定义初始化
     */
    protected abstract void doInit();

    public final void refresh(AirChannelProperties properties) {
        // 判断是否更新
        if (properties.equals(this.properties)) {
            return;
        }
        log.info("[refresh][配置({})发生变化，重新初始化]", properties);
        this.properties = properties;
        // 初始化
        this.init();
    }
    
    @Override
    public Long getId() {
        return properties.getId();
    }

    @Override
    public String getName() {
        return properties.getName();
    }

}
